GBML manual / Lists

list_create_fill

Makes a list that can hold up to capacity items and fills it right away: it ends up with capacity items, every one of them value. It is list_create followed by list_fill, in one line. Unlike list_create, the value you give is really put in - it is not just an example. The value also decides what kind of list it is: list_create_fill(13, 0) holds whole numbers and list_create_fill(13, 0.0) holds decimals. A whole number fits in a list of decimals but a decimal does not fit in a list of whole numbers, so write 0.0 if the list will ever hold a decimal (an answer from lerp, say). A `/// @type {list}` comment above the line also makes it decimals. capacity must be a whole number written right there in the code.

Syntax

list_create_fill(capacity, value) -> list<T>

Arguments

NameType
capacityint
valueelement

Returns

list<T>

Description

Makes a list with room for capacity items and fills it right away, so it holds capacity items and every one of them is value. list_create_fill(12, "HI") is a list of text with room for 12 that already has twelve "HI" in it.

It is two steps in one line. These two do the same job:

var names = list_create(12, "");     // an empty list...
list_fill(names, "HI");              // ...and then fill it
var names = list_create_fill(12, "HI");

It is the value, not an example

list_create takes an example as its second value. The example only tells GBML what kind of thing goes in the list. It is never put in, and the list starts empty.

list_create_fill takes a value, and the value does two jobs at once. It tells GBML what kind of thing the list holds (0 is whole numbers, 0.0 decimals, "" text, true yes-or-no, { x: 0, y: 0 } a struct), and it is put in every place. There is no separate example to leave out: the one value is both. If you are wondering where the example went, this is it.

The value decides what kind of list it is

The kind of list follows from the value you give, so the value has to be the kind you want:

You writeYou get
list_create_fill(13, 0)a list of whole numbers
list_create_fill(13, 0.0)a list of decimals
list_create_fill(13, "")a list of text
list_create_fill(13, true)a list of yes-or-no values

0 and 0.0 look almost the same, but they make different lists. This is the mistake that is easiest to make: if the list is going to hold decimals, such as the answers from lerp, a division or anything with a point in it, write the value with a point, like 0.0.

The two kinds do not mix evenly. A whole number goes into a list of decimals without any trouble - 5 becomes 5.0. A decimal does not go into a list of whole numbers, because there is no room for the part after the point. So a list made with list_create_fill(13, 0) is a list of whole numbers, and later writing items[2] = lerp(items[2], 5.5, 0.5); is a mistake, and GBML tells you about it when the project is checked. The error points at the line that wrote the decimal, and it also says which line made the list hold whole numbers, even when that is in a different event, so you know where to fix it.

There are two ways to put it right:

It starts full

A list made this way has as many items as its capacity, so list_length gives the same number as the capacity, and names[0] up to names[11] all work straight away. A loop like for (var i = 0; i < 12; i++) { names[i] = "x"; } is safe to run on it.

A full list has no room left. list_add on it gives back false and adds nothing, until you take something out with list_remove or empty it with list_clear. If you want a list you can add to from the start, use list_create, which starts empty.

What happens in the awkward cases

You do thisWhat happens
leave capacity out, or give three argumentsthe project is refused when it is checked, and the message shows how to write it
give capacity as a variable, like list_create_fill(n, 0)refused. The size is fixed when the game is built, so it has to be a number you type
give a capacity of 0, or one with a decimal pointrefused. A list has room for at least 1
give nothing as the valuerefused. nothing does not say what kind of thing the list holds
write it anywhere but the whole right-hand side of the line that makes the variablerefused. See below
write the two arguments the wrong way round, like list_create_fill("HI", 12)refused, and the message says the capacity comes first
say the list holds one kind and give a value of another (/// @type {list<string>} over list_create_fill(3, 0))refused: a variable keeps one kind
write a decimal into a list of whole numbers, like items[2] = 1.5refused. The message says which line made the list hold whole numbers and what to write there instead

Where it can go

Like list_create, it sets the size of the variable it is given to. It goes on the right-hand side of the line that makes the variable and nowhere else:

Not inside another call or a sum, not on a line by itself, not in a later event such as Step, and not over a variable that already exists. To fill a list that already exists, use list_fill.

Why only lists

array_create and grid_create already fill what they make, so they are the array and grid versions of this. There is no stack_create_fill, queue_create_fill, set_create_fill or map_create_fill on purpose. A stack or queue that starts out holding twelve of the same thing is not something a game can do much with. A set holds each value once, so twelve copies of one value would be a set of one. And a map needs keys as well as values, which one value cannot give.

What it costs

The list takes room for capacity items whether or not you use them all, the same as any list. Filling it walks through every place once, so it takes time in proportion to the capacity. Do it once when something starts, not every frame.

Example

var names = list_create_fill(3, "HI");   // a list of text with room for 3: HI, HI, HI
var how_many = list_length(names);       // 3 - it starts full
names[2] = "BYE";                        // place 2 is already there, so this is fine

A list of numbers that starts at zero in every place:

var counts = list_create_fill(10, 0);    // ten places, all 0
counts[4] = counts[4] + 1;               // add one to place 4

On the GBA

Compiles to a fixed-size vector of `capacity` items filled by the GBA module's own count-and-value constructor (a temporary vector of N copies of the value, then the variable is made from it) - exactly what array_create does, except the variable is a list, so it can still be added to, removed from and emptied afterwards. The same storage `/// @type {list<T>}` + `/// @capacity capacity` gives, so it is not slower or bigger than writing the two lines. The element kind is the value's own (0 is a vector of whole numbers, 0.0 a vector of decimals) unless the variable is declared as decimals (`/// @type {list<real>}`, or `list<real, N>`) over a whole-number value, in which case the list is built with decimal (fixed-point) elements and the value converted - the declared kind wins, but only in that one direction (a decimal value cannot be put into a list declared as whole numbers). The size has to be a plain number because it becomes a C++ template parameter: the Game Boy Advance has no heap to hand out memory while the game runs, so every container is sized when the ROM is built (anything else is refused). It must be the whole right-hand side of a declaration, because that is the one place a size can be attached to a variable. value is a real value here: it is evaluated once and copied into every place, and its kind decides what the list holds. A list that is full has no room for list_add until something is removed. Only the list has a create-and-fill: array_create and grid_create already fill, and a stack, queue, set or map pre-filled with one value is not something a game can use.

See also: list_createlist_filllist_lengthlist_addlist_copyarray_creategrid_createListsData containers